package com.youxin.jobcenter;

import javax.annotation.PostConstruct;

import org.apache.log4j.BasicConfigurator;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.youxin.busfoundation.common.BuildConfig;
import com.youxin.busfoundation.common.HandleShutdownApp;
import com.youxin.busfoundation.config.EsConfig;
import com.youxin.busfoundation.config.KafkaConfig;
import com.youxin.busfoundation.config.RedisClusterConfig;
import com.youxin.busfoundation.config.WebLoadBalancer;
import com.youxin.busfoundation.config.WxBeanConfig;
import com.youxin.busfoundation.config.ZfbBeanConfig;
import com.youxin.foundation.db.redis.RedisClusterCommand;
import com.youxin.foundation.utils.thread.BaseThreadPool;
import com.youxin.foundation.utils.thread.ScheduleThreadPool;
import com.youxin.jobcenter.timers.PayStatusCheckJob;


/**
 * 启动入口
 * 
 * @author yingde.cao
 *
 */
@SpringBootApplication()
@EnableEurekaClient
@RestController
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableTransactionManagement
@EnableFeignClients
@EnableDiscoveryClient
@ComponentScan(basePackages={"com.youxin.jobcenter","com.youxin.cpcenter"})
@EnableConfigurationProperties({KafkaConfig.class,EsConfig.class
	,WxBeanConfig.class,ZfbBeanConfig.class,RedisClusterConfig.class})@MapperScan("com.youxin.cpcenter.dao")
@Configuration
public class JobCenterApplication {
	
	@Autowired 
	private EsConfig esConfig;
	@Autowired
	private KafkaConfig kafkaConfig;
	@Autowired
	private Environment environment;
	@Autowired
	private RedisClusterConfig redisClusterConfig;
	
	@Value("${eureka.client.serviceUrl.defaultZone}")
	private String eurekaServer;
	@Value("${spring.datasource.url}")
	private String dataSourceUrl;
	@Autowired
	private LoadBalancerClient loadBalancer;
	@Autowired
	private ZfbBeanConfig zfbBeanConfig;
	@Autowired
	private WxBeanConfig wxBeanConfig;
	@Autowired 
	private PayStatusCheckJob payStatusCheckJob;
//	@Autowired
//	private DataSource dataSource;
//	
//	@Autowired
//	private JdbcTemplate jdbcTemplate;
	
	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(JobCenterApplication.class);
	public static void main(String[] args) {
		// BasicConfigurator.configure(); //防止出现log4j: WARN no
		// appender警告，也可配制解决。
		BasicConfigurator.configure();
		SpringApplication.run(JobCenterApplication.class, args);
	}
	
	@Autowired
	private Environment env;
	@Value("${server.port}")
	String port;
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	/**
	 * 打印当前
	 */
	private void getActiveProfiles() {
		logger.warn("******加载配置,dbURL:"+dataSourceUrl+",EurekaServer:"+eurekaServer);
		for (final String profileName : environment.getActiveProfiles()) {
			logger.warn("******当前使用的配置 - " + profileName);
		}
	}

	@PostConstruct
	private void init() {		
		try {		
			WebLoadBalancer.initWithBanlancer(loadBalancer);
			getActiveProfiles();
			KafkaConfig.initConfig(kafkaConfig);	
			EsConfig.initConfig(esConfig);
			RedisClusterCommand.initConfig(redisClusterConfig);
			// 初始化 支付宝 微信 参数
			ZfbBeanConfig.initWithConfig(zfbBeanConfig);
			WxBeanConfig.initWithConfig(wxBeanConfig);
			ScheduleThreadPool.instance(6);
			BaseThreadPool.instance(10,Integer.MAX_VALUE, 1000);
			BuildConfig.initWithEnvid(env);
			//定时任务
			payStatusCheckJob.execute();
			try {
				HandleShutdownApp.instance().handleSignal(()->{
				}
				);
			} catch (Exception e) {
				logger.error("加载HandleSignal异常", e);			
			}
		} catch (Exception e) {
			logger.error("加载异常", e);	
		}	
	}
}
